/**
 * @author LKQ
 * @date 2022/2/26 16:37
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int beautySum(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        int totalSum = 0;
        for (int i = 0; i < n; i++) {
            int[] cnt = new int[26];
            for (int j = i; j < n; j++) {
                cnt[chars[j]-'a']++;
                int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
                for (int k: cnt) {
                    if (k > 0) {
                        max = Math.max(max, k);
                        min = Math.min(min, k);
                    }
                }
                totalSum += max - min;
            }
        }
        return totalSum;
    }
}
